package com.wzh.user.biz.sysUser.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.general.result.PageQuery;
import com.wzh.common.general.result.PageResult;
import com.wzh.common.mybatis.util.MybatisPageUtils;
import com.wzh.user.api.dto.SysUserDTO;
import com.wzh.user.biz.sysUser.domain.entity.SysUser;
import com.wzh.user.biz.sysUser.mapper.SysUserMapper;
import com.wzh.user.biz.sysUser.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 系统用户表服务实现类
 *
 * @author system
 * @since 2025-06-18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Override
    public PageResult<SysUserDTO> page(PageQuery<SysUserDTO> pageQuery) {
        // 初始化分页条件
        Page<SysUser> page = MybatisPageUtils.createPage(pageQuery);
        // 处理排序和高级查询
        LambdaQueryWrapper<SysUser> queryWrapper = MybatisPageUtils.handleOrderByAndQueryLambda(pageQuery, "create_time");
        // 查询
        Page<SysUser> result = page(page, queryWrapper);
        // 返回DTO
        return MybatisPageUtils.toPageResult(result, SysUserDTO.class);
    }

    @Override
    public Boolean checkUserDuplicate(SysUserDTO dto) {
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(CharSequenceUtil.isNotBlank(dto.getUsername()), SysUser::getUsername, dto.getUsername());
        sysUserLambdaQueryWrapper.eq(ObjectUtil.isNotNull(dto.getEmail()), SysUser::getEmail, dto.getEmail());
        sysUserLambdaQueryWrapper.eq(ObjectUtil.isNotNull(dto.getMobile()), SysUser::getMobile, dto.getMobile());
        return count(sysUserLambdaQueryWrapper) > 0;
    }
}